import { BaseTargetData_blxf } from "../GameData/TargetData";
import { EnumManager_blxf } from "../Manager/EnumManager";
import { LoadModule_blxf } from "../Module/LoadModule";
import { NumModule_blxf } from "../Module/NumModule";
import { UtilsModule_blxf } from "../Module/UtilsModule";


const {ccclass, property} = cc._decorator;

/** 菜篮子item */
@ccclass
export default class TargetItem_blxf extends cc.Component {

    @property({ type: cc.Node, tooltip: '盒子' })
    nodeBox: cc.Node = null;

    @property({ type: cc.Sprite, tooltip: '已解锁菜篮子' })
    bgImgSpr1: cc.Sprite = null;

    @property({ type: cc.Sprite, tooltip: '未解锁菜篮子' })
    bgImgSpr2: cc.Sprite = null;

    @property({ type: cc.Node, tooltip: '篮子锁' })
    unlockBox: cc.Node = null;

    @property({ type: cc.Sprite, tooltip: '解锁图标' })
    unlockIconSpr: cc.Sprite = null;

    @property({ type: cc.Label, tooltip: '解锁数量' })
    unlockNumText: cc.Label = null;

    @property({ type: cc.Node, tooltip: '商品背景' })
    shopBgNode: cc.Node = null;

    @property({ type: cc.Sprite, tooltip: '商品图片' })
    shopImgSpr: cc.Sprite = null;

    @property({ type: cc.Label, tooltip: '菜篮子进度' })
    needNumText: cc.Label = null;

    @property({ type: cc.Node, tooltip: '随便来点' })
    normalText: cc.Node = null;

    @property({ type: cc.Node, tooltip: '解锁背景' })
    unlockBg: cc.Node = null;

    @property({ type: cc.Node, tooltip: '解锁内容盒子' })
    unlockContentBox: cc.Node = null;

    @property({ type: cc.Node, tooltip: '解锁文字' })
    unlockText: cc.Node = null;

    @property({ type: cc.Node, tooltip: '锁图标' })
    unlockIcon: cc.Node = null;

    @property({ type: cc.Node, tooltip: '菜篮盖子' })
    offNode: cc.Node = null;

    @property({ type: cc.Node, tooltip: '商品盒子' })
    shopBox: cc.Node = null;


    @property({ tooltip: '传入参数' })
    _data: BaseTargetData_blxf = null;

    @property({ tooltip: '是否已加载锁动画' })
    _isLoadAnimation: boolean = false;

    @property({ tooltip: '锁动画' })
    _unlockAnimation: cc.Node = null;

    @property({ tooltip: '运行帧数'})
    _dtNum: number = 0;

    @property({ tooltip: '商品售卖价格' })
    _shopData: any = {};


    @property({ tooltip: '商品图片隐藏时间' })
    _shopImgHideTime: number = 0;


    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    start () {

    }

    update ( dt: number ) {
        this._dtNum += 1;

        if ( !this._data ) return;

        this.onCheckShopImgOpacity_blxf( dt );

        this._dtNum % 5 == 1 && this.onCheckIsUnlock_blxf();
    }

    initData_blxf ( data: BaseTargetData_blxf ) {
        this._data = data;

        this.updateUI_blxf();

        this.updateShopData_blxf( true );

        this.initEvent_blxf();
    }

    initEvent_blxf () {
        let { targetId, isAuto } = this._data;
        cc.game.on(`updateImgScale_blxf_${ targetId }`, this.updateImgScale_blxf, this);

        isAuto && cc.game.on(`onAutoTargetShopData_${targetId}`, this.onAutoTargetShopData, this);
        isAuto && cc.game.on(`updateShopImgOpacity_blxf_${targetId}`, this.updateShopImgOpacity_blxf, this);
    }

    // 更新商品显隐
    updateShopImgOpacity_blxf ( isShow: boolean = false ) {
        this.shopImgSpr.node.opacity =  isShow ? 255 : 0;

        if ( isShow ) this._shopImgHideTime = 0;
    }

    onAutoTargetShopData ( shopData: any ) {
        let { SellNum, AtlasName, ImgName } = shopData;
        this._shopData = shopData;

        let { autoTargetData } = this._data;
        let { nowLevelData } = autoTargetData || {};
        let { SaleAddNum } = nowLevelData || {};

        let _num = Number(SellNum) + Number(SaleAddNum);
        UtilsModule_blxf.instance.setText_blxf( this.unlockNumText.node, `${_num}` );

        this.shopImgSpr.node.active = true;
        let _options = { imgSpr: this.shopImgSpr, path: AtlasName, imgName: ImgName };
        LoadModule_blxf.instance.LoadImg_blxf( _options );

        this.updateShopImgOpacity_blxf( false );
    }

    /** 更新节点显示 */
    updateNode_blxf ( isShow: boolean ) {
        this.nodeBox.opacity = isShow ? 255 : 0;
    }

    /** 检测商品显隐（处理商品图片不显示问题） */
    onCheckShopImgOpacity_blxf ( dt: number ) {
        let { isAuto, targetType } = this._data;
        if ( isAuto ) return;

        let _isUnlock: boolean = this.onCheckUnlock_blxf();
        if ( !_isUnlock ) return;

        let _isHide = this.shopImgSpr.node.opacity == 0;
        if ( !_isHide ) return;

        this._shopImgHideTime += dt;

        if ( this._shopImgHideTime < 3 ) return;

        this.updateShopImgOpacity_blxf( true );
    }

    /** 更新UI */
    updateUI_blxf () {
        let { targetType, unlockType, unlockNum } = this._data;
        let _isUnlock: boolean = this.onCheckUnlock_blxf();

        let { isAuto, autoTargetData } = this._data;
        let { nowLevelData, checkNum } = autoTargetData || {};
        let { SaleAddNum } = nowLevelData || {};
        if ( isAuto ) {
            let { NUM_GOLD, NUM_DIAMOND } = EnumManager_blxf.gameNumType;
            unlockType = _isUnlock ? NUM_GOLD : NUM_DIAMOND, unlockNum = SaleAddNum;

            if ( !_isUnlock ) unlockNum = checkNum;
        }

        let _shopId: any = this._data.getBuyShopId_blxf();
        let _isShop: boolean = _shopId && _shopId != -1;
        let _isVideo: boolean = unlockType == 9;

        this.updateNode_blxf(true);

        // 自动售卖相关节点
        this.offNode.active = !_isUnlock;
        this.unlockBox.active = !_isUnlock /*|| _isUnlock && isAuto*/;
        // this.shopBgNode.active = _isUnlock && targetType != 1 && _isShop;

        this.shopBgNode.active = _isUnlock && isAuto && false;
        this.shopImgSpr.node.active = _isUnlock && !_isVideo;
        // if ( isAuto ) this.shopImgSpr.node.y = 40;

        this.needNumText.node.active = _isUnlock && _isVideo;
        this.normalText.active = _isUnlock && (Number(targetType) <= 2 && !_isShop || _isVideo);
        this.unlockBg.active = _isVideo && !_isUnlock;
        this.unlockContentBox.active = !_isUnlock;
        this.unlockText.active = _isVideo && !_isUnlock;
        this.unlockIcon.active = !_isVideo;
        if ( !this.unlockText.active ) {
            this.unlockIcon.y = 2;
            this.unlockIcon.scale = 1.4;
        }

        if ( !_isUnlock || _isUnlock && isAuto ) {
            
            this.unlockNumText.node.active = !_isVideo && !_isUnlock;
            UtilsModule_blxf.instance.setText_blxf( this.unlockNumText.node, `${unlockNum}` );

            let _imgBox: cc.Node = this.unlockIconSpr.node.parent;
            if ( _isVideo ) _imgBox.width = 50;
            UtilsModule_blxf.instance.loadIconImg_blxf( unlockType, this.unlockIconSpr, _imgBox );
        }
    }

    updateImgScale_blxf ( isShow: boolean ) {
        this.shopImgSpr.node.opacity = isShow ? 1255 : 0;
    }

    /**
     * 更新菜篮子商品相关数据
     * @param isTargetLv 菜篮子是否升级
     */
    updateShopData_blxf ( isTargetLv: boolean = false ) {
        this.updateNode_blxf( true );

        this.updateNum_blxf();
        this.updateShop_blxf();
        
        this.updateUI_blxf();

        isTargetLv && this.updateBgImg_blxf();

        !isTargetLv && this.updateImgScale_blxf( false );
    }

    /** 更新菜篮子 */
    updateBgImg_blxf () {
        let { AtlasName, ImgName_on, ImgName_off } = this._data.targetLvData;
        let path: string = `${AtlasName}`;
        let _options1: object = { path, imgName: ImgName_on, imgSpr: this.bgImgSpr1, cb: ()=>{
            let _options2: object = { path, imgName: ImgName_off, imgSpr: this.bgImgSpr2 };
            LoadModule_blxf.instance.LoadImg_blxf( _options2 );
        }};
        LoadModule_blxf.instance.LoadImg_blxf( _options1 );
    }

    /** 更新菜篮子数 */
    updateNum_blxf ( shopNum: number = 0 ) {
        let _needBuyNum: string = this._data.getShopNumStr_blxf( shopNum );
        UtilsModule_blxf.instance.setText_blxf( this.needNumText.node, `${_needBuyNum}` );
    }

    /** 更新菜篮子商品数据 */
    updateShop_blxf () {
        let _shopId: any = this._data.getBuyShopId_blxf();

        this.updateShopImg_blxf( _shopId );

        if ( !_shopId || _shopId == -1 ) {
            this.shopImgSpr.spriteFrame = null;
            this.shopImgSpr.node.active = false;
        }
        // cc.warn('_shopId >>', _shopId, this.shopImgSpr.spriteFrame);

        this.updateUI_blxf();
    }

    /** 更新商品图片 */
    updateShopImg_blxf ( _shopId ) {
        if ( !_shopId || _shopId == -1 ) return;

        let { isAuto, buyShopInfo } = this._data;
        if ( isAuto ) return;

        let { shopAtlasName, shopImgName } = buyShopInfo;

        this.shopImgSpr.node.active = true;
        let _options = { imgSpr: this.shopImgSpr, path: shopAtlasName, imgName: shopImgName };
        LoadModule_blxf.instance.LoadImg_blxf( _options );
    }

    /** 检测是否解锁 */
    onCheckUnlock_blxf () {
        return this._data.onCheckUnlockTarget_blxf();
    }

    /** 获取菜篮子id */
    getTargetId_blxf () {
        let { targetId } = this._data;
        return targetId;
    }

    /** 获取菜篮子商品id */
    getBuyShopId_blxf () {
        return this._data.getBuyShopId_blxf();
    }

    /** 检测标记商品是否可放入菜篮子 */
    onCheckShopId_blxf ( checkShopId: number ) {
        return this._data.onCheckShopId_blxf( checkShopId );
    }

    getTargetLv_blxf () {
        return this._data.getTargetLv_blxf();
    }

    initTargetLvUpData_blxf () {
        this._data.initTargetLvUpData_blxf();
    }

    /** 检测是否可解锁 */
    onCheckIsUnlock_blxf () {
        if ( this.onCheckUnlock_blxf() ) return this.onClearUnlockAnimation_blxf();

        let { unlockType, unlockNum } = this._data;

        if ( unlockType == 9 ) return;

        let _isNum: boolean = NumModule_blxf.instance.onCheckNum_blxf( unlockType, unlockNum );
        if ( !_isNum ) return this.onClearUnlockAnimation_blxf();

        // 自动售卖
        let { isAuto, autoTargetData } = this._data;
        if ( isAuto ) {
            let { consumeType, consumeNum } = autoTargetData;
            let _isNum2: boolean = NumModule_blxf.instance.onCheckNum_blxf( consumeType, consumeNum );
            if ( !_isNum2 ) return this.onClearUnlockAnimation_blxf();
        }

        this.unlockContentBox.active = false;
        if ( this._isLoadAnimation ) return;

        this._isLoadAnimation = true;

        let { SPINE_GAME_LOCK } = EnumManager_blxf.gameSpineType;
        UtilsModule_blxf.instance.createSpine_blxf( SPINE_GAME_LOCK, this.unlockBox, ( spineSkeleton: sp.Skeleton )=>{
            this._unlockAnimation = spineSkeleton.node;
            this._unlockAnimation.scale = 0.7;
        });
    }

    /** 清除动画 */
    onClearUnlockAnimation_blxf () {
        if ( !this._isLoadAnimation ) return;

        this._isLoadAnimation = false;
        if ( this._unlockAnimation ) {
            this._unlockAnimation.destroy();
        }
        this._unlockAnimation = null;
        this.unlockContentBox.active = true;
    }

    /** 获取是否可解锁 */
    getIsLoadAnimation_blxf () {
        this.onCheckIsUnlock_blxf();
        
        return this._isLoadAnimation;
    }
}
